<?php

  class Boolean extends Base implements IBoolean {

    private $boolean;

    public function __construct($boolean) {
      if (is_bool($boolean)) {
        $this->boolean = $boolean;
      } else {
        throw new IllegalArgumentException("\$boolean parameter must be a boolean.");
      }
    }

    public function __invoke() {
      return $this->boolean;
    }

    public function __toString() {
      return $this->boolean == TRUE ? new String("true") : new String("false");
    }

    /**
     * Returns an integer according to current boolean value.
     * @return IInteger The integer value.
     */
    public function toInt() {
      return $this->boolean == TRUE ? new Integer(1) : new Integer(0);
    }

    public function booleanValue() {
      return $this->boolean;
    }

    public function compareTo(IBoolean $boolean) {
      $return = null;
      if ($this->boolean === $boolean->booleanValue()) {
        $return = 0;
      } else if ($this->boolean === true && $boolean->booleanValue() === false) {
        $return = 1;
      } else if ($this->boolean === false && $boolean->booleanValue() === true) {
        $return = -1;
      }
      return new Integer($return);
    }

    public static function parseBoolean($string) {
      if (strtolower($string) == "true" || $string == "1") {
        return new Boolean(true);
      } else {
        return new Boolean(false);
      }
    }

  }

?>
